<template>
<div class="jieyue">
    <div class="top">
      <div >借阅管理</div>
      <hr/>
      <div class="flex gap-4 mb-4">
      <span >借阅人</span>
    <el-input
      v-model="jieyuename"
      style="width: 240px"
      placeholder="请输入"
      :suffix-icon="Calendar"
    />
    <span>图书名称</span>
    <el-input
      v-model="bookname"
      style="width: 240px"
      placeholder="请输入"
      :prefix-icon="Search"
    />
    <el-button type="primary" @click="chaxun">查询</el-button>
    <el-button type="defaut" @click="chongzhi">重置</el-button>

  </div>
    </div>


    <div class="centers">
      <el-table :data="filterTableData" style="width: 100%">
    <el-table-column label="图书名称" prop="bookName" />
    <el-table-column label="借阅时间" prop="getTime" />
    <el-table-column label="归还时间" prop="reTime" />
    <el-table-column label="借阅状态" prop="jieStatus" />
    <el-table-column label="借阅人" prop="username" />
    <el-table-column align="right">
      <template #default="scope">
        <el-button size="small"  type="primary" @click="jieyue(scope.row)" :disabled="scope.row.username !=''">
          借阅
        </el-button>
        <el-button
          size="small"
          type="primary"
          @click="guihuan(scope.row)"
          :disabled="scope.row.username ==''"
        >
          归还
        </el-button>
      </template>
    </el-table-column>
  </el-table>
    </div>
    <div class="demo-pagination-block">
    <el-pagination
      v-model:current-page="currentPage4"
      v-model:page-size="pageSize4"
      :page-sizes="[]"
      :size="size"
      :disabled="disabled"
      :background="background"
      layout="total, sizes, prev, pager, next, jumper"
      :total="totals"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</div>
</template>
<script setup>

import axios from 'axios';
import { ref, computed, onMounted } from 'vue';
const totals = ref(1)
const tableData = ref([])
const search = ref('')
const filterTableData = computed(() =>
  tableData.value.filter(
    (data) =>
      !search.value ||
      data.name.toLowerCase().includes(search.value.toLowerCase())
  )
)


const currentPage4 = ref(1)
const pageSize4 = ref(10)
const size = ref(10)
const background = ref(false)
const disabled = ref(false)
const jieyuename = ref('')
const bookname = ref('')
const canshu = ref({
    current: currentPage4.value,
    size: size.value,
    jieYueName: jieyuename.value,
    bookName: bookname.value
})
const getBook = () =>{
  axios.get("http://127.0.0.1:8080/jieyue/get",{
   params:{
    ...canshu.value
   }
  }).then(res =>{
    tableData.value = res.data.data.records
    totals.value = res.data.data.total
  })
}
import { useRoute } from 'vue-router';
const route = useRoute()
const username = ref('')
onMounted(() =>{
  username.value = route.query.username
  getBook()
})


const jieyue = (e) =>{
  e.username = username.value
  console.log(e);
  axios.put("http://127.0.0.1:8080/jieyue/jieyue",e).then(res =>{
    console.log(res);
    getBook()
  })
}
const guihuan = (e) =>{
  e.username = ''
  axios.put("http://127.0.0.1:8080/jieyue/guihuan",e).then(res =>{
    console.log(res);
    getBook()
  })
}
const chaxun = () =>{
  canshu.value.jieYueName = jieyuename.value
  canshu.value.bookName = bookname.value
  getBook()

}
const chongzhi = () =>{
  jieyuename.value = ''
  bookname.value = ''
  canshu.value.jieYueName = jieyuename.value
  canshu.value.bookName = bookname.value
  getBook()
}
</script>
<style scoped>
.top{
  margin-bottom: 20px;
}
</style>